<?php defined('SYSPATH') or die('No direct script access.');

class Model_Season extends Model_Filter
{  
    const SUMMER_SEASON   = 1;
    const DEMI_SEASON     = 2;
    const MULTI_SEASON    = 3;
    const WINTER_SEASON   = 4;
    
    public static $seasons = array(
        'leto'              => self::SUMMER_SEASON,
        'demisezon'         => self::DEMI_SEASON,
        'kruglogodichnyiy'  => self::MULTI_SEASON,
        'zima'              => self::WINTER_SEASON,
    );
    
    public static $season_options = array(       
        self::SUMMER_SEASON => array(
            'name'        => 'лето',
            'single_form' => 'на лето',
            'plural_form' => 'летние',
            'genitive_form' => 'летних',
        ),
        self::DEMI_SEASON => array(
            'name'        => 'демисезон',
            'single_form' => 'демисезон',
            'plural_form' => 'демисезонные',
            'genitive_form' => 'демисезонных',
        ),
        self::MULTI_SEASON => array(
            'name'        => 'круглогодичный',
            'single_form' => 'мультисезон',
            'plural_form' => 'круглогодичные',
            'genitive_form' => 'круглогодичных',
        ),
        self::WINTER_SEASON => array(
            'name'        => 'зима',
            'single_form' => 'на зиму',
            'plural_form' => 'зимние',
            'genitive_form' => 'зимних',
        ),
    );
    
    public static function get_seasons($category = null, $brand = null, $color = null, $composition = null, $season = null, $shop = null, $price = null, $use_denormalization = true, $disable_cache = false)
    {
        $ids = array();
        $category_id = self::_get_category_id($category, $ids);
        
        $brand_id    = self::_get_brand_id($brand);
        
        $color_id    = self::_get_color_id($color);
        
        $composition_id = self::_get_composition_id($composition);
        
        $season_id   = self::_get_season_id($season);
        
        $shop_id     = self::_get_shop_id($shop);
        
        $from_price  = self::_get_from_price($price);
        $to_price    = self::_get_to_price($price);
        
        if (!$disable_cache && $seasons = Cache::instance()->get('catalog_seasons_'.$category_id.'_'.$brand_id.'_'.$color_id.'_'.$composition_id.'_'.$shop_id.'_'.$from_price.'_'.$to_price)) {
            return $seasons;
        }
        
        $isset_denormalized_data = null;
        $use_denormalization     = false; 
        if ($use_denormalization) {
            // Узнаем, используется ли денормализация
            $isset_denormalized_data = self::_isset_denormalized_data($category_id, $brand_id, $color_id, $composition_id, $season_id, $shop_id, $price);   
        }
        
        $stat = DB::select('season_id', array(DB::expr('COUNT( * )'), 'count'))
                    ->from('products')
                    ->where('is_available', '=', 1)
                    ->where('season_id', '!=', 0);
                    
        if (is_array($ids) && count($ids) > 0) {
            $stat->where('category_id', 'IN', $ids);
        }
                    
        if ($brand_id) {
            $stat->where('brand_id', '=', $brand_id); 
        }   
             
        if ($color_id) {
            $stat->where('color_id', '=', $color_id);   
        }
        
        if ($composition_id) {
            $stat->where('composition_id', '=', $composition_id);   
        }
        
        if ($shop_id) {
            $stat->where('shop_id', '=', $shop_id);    
        }
        
        if ($from_price) {
            $stat->where('price', '>=', $from_price);    
        }
        if ($to_price) {
            $stat->where('price', '<=', $to_price); 
        }
        
        if ($use_denormalization && $isset_denormalized_data) {
            $stat = self::_get_denormalized_data('season_id', $category_id, $brand_id, $color_id, $composition_id, null, $shop_id, $price);    
        } else {              
            $stat = $stat
                    ->group_by('season_id')
                    ->order_by('count', 'DESC')
                    ->execute()
                    ->as_array();
        }
        
        $seasons = array();
        $all_seasons = self::get_all_seasons();
        foreach ($stat as $season) {
            $seasons[$season['season_id']] = $all_seasons[$season['season_id']];
            $seasons[$season['season_id']]['count'] = $season['count'];            
        } 
        usort($seasons, array(__CLASS__, '_compare'));
        
        Cache::instance()->set('catalog_seasons_'.$category_id.'_'.$brand_id.'_'.$color_id.'_'.$composition_id.'_'.$shop_id.'_'.$from_price.'_'.$to_price, $seasons);
        return $seasons;     
    }
    
    public static function get_all_seasons()
    {
        $seasons = array();
        foreach (self::$seasons as $url => $value) {
            $seasons[$value] = self::$season_options[$value];
            $seasons[$value]['url'] = $url;
            $seasons[$value]['id']  = $value;        
        } 
        return $seasons;   
    }
    
    public static function get_season_by_url($url)
    {
        $seasons = self::get_all_seasons();
        foreach ($seasons as $season) {
            if ($season['url'] == $url) {
                return $season;    
            }    
        }
        return null;
    }
}